package com.xy.util;

import android.media.ExifInterface;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;

/**
 * Created by XuYang
 * 2023/4/21
 * Email:544066591@qq.com
 */
public class FileUtil {

    public static void delete(String path) {
        File file = new File(path);
        delete(file);
    }

    public static void delete(File file) {
        if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            } else if (file.isDirectory()) {
                File[] files = file.listFiles();
                if (files == null || files.length == 0) {
                    file.delete();
                } else {
                    for (int i = 0; i < files.length; i++) {
                        delete(files[i]);
                    }
                    file.delete();
                }
            }
        }
    }

    public static String getFileMD5(File file) {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte[] buffer = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            String s = file.getName() + file.length();
            byte[] bytes = digest.digest(s.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    public static boolean isExists(File file) {
        return file.exists();
    }

    public static String getEarliestFolder(String path) {
        File directory = new File(path);
        File[] files = directory.listFiles();
        long earliestFolderTime = Long.MAX_VALUE;
        File earliestFile = null;

        if (files != null) {
            for (File file : files) {
                if (file.lastModified() < earliestFolderTime) {
                    earliestFile = file;
                    earliestFolderTime = file.lastModified();
                }
            }
        }

        if (earliestFile != null) {
            return earliestFile.getPath();
        } else {
            return "";
        }
    }
}
